package cn.team11.smarteye.module;

import android.content.Context;


import org.apache.commons.lang3.StringUtils;

import java.util.List;

import cn.team11.sds.dayi.SampleApplication;
import cn.team11.smarteye.bean.AlarmMsgInfoBean;
import cn.team11.smarteye.bean.AlarmMsgInfoBeanDao;
import cn.team11.smarteye.bean.CateyeInfoBean;
import cn.team11.smarteye.bean.CateyeInfoBeanDao;
import cn.team11.smarteye.bean.DaoSession;
import cn.team11.smarteye.bean.RingRecordBean;
import cn.team11.smarteye.bean.RingRecordBeanDao;

/**
 * Created by Murphy on 2017/11/10.
 */

public class MyDaoManager {

    private static MyDaoManager instance;
    private static Context appContext;
    private DaoSession mDaoSession;
    private CateyeInfoBeanDao mCateyeInfoBeanDao;
    private AlarmMsgInfoBeanDao mAlarmMsgBeanDao;
    private RingRecordBeanDao mRingRecordBeanDao;

    private MyDaoManager(){}

    /**
     * 采用单例模式
     *
     * @param context 上下文
     * @return dbservice
     */
    public static MyDaoManager getInstance(Context context) {
        if (instance == null) {
            instance = new MyDaoManager();
            if (appContext == null) {
                appContext = context.getApplicationContext();
            }
            instance.mDaoSession = SampleApplication.instances.getDaoSession();
            instance.mCateyeInfoBeanDao = instance.mDaoSession.getCateyeInfoBeanDao();
            instance.mAlarmMsgBeanDao = instance.mDaoSession.getAlarmMsgInfoBeanDao();
            instance.mRingRecordBeanDao = instance.mDaoSession.getRingRecordBeanDao();
        }
        return instance;
    }

    /**
     * 插入一条新的报警信息
     * */
    public void insertAlarmMsg(AlarmMsgInfoBean msg){
        mAlarmMsgBeanDao.insertOrReplace(msg);
    }

    /**根据aid获取一条报警信息*/
    public AlarmMsgInfoBean getAlarmMsg(String aid){
        return mAlarmMsgBeanDao.queryBuilder().where(AlarmMsgInfoBeanDao.Properties.Aid.eq(aid)).build().unique();
    }

    public void updateAlarmMsg(AlarmMsgInfoBean msg){
        if (msg != null)
            mAlarmMsgBeanDao.update(msg);
    }

    public void updateAlarmMsgReadableStatus(String aid, boolean readable){
        AlarmMsgInfoBean alarmItem = getAlarmMsg(aid);
        if (alarmItem != null){
            alarmItem.setMarkAsRead(readable);
            updateAlarmMsg(alarmItem);
        }

    }

    public void deleteAlarmMsg(AlarmMsgInfoBean info){
        if (info != null)
            mAlarmMsgBeanDao.delete(info);
    }

    public void deleteAllAlarms(){
        mAlarmMsgBeanDao.deleteAll();
    }

    /**
     * 获取指定时间范围内的所有报警信息
     * */
    public List<AlarmMsgInfoBean> getTargetAlarmMsg(String bid, Long minTime, Long maxTime){
        return mAlarmMsgBeanDao.queryBuilder()
                .where(AlarmMsgInfoBeanDao.Properties.Time.between(minTime, maxTime),
                        AlarmMsgInfoBeanDao.Properties.Bid.eq(bid))
                .build().list();
    }

    public List<AlarmMsgInfoBean> getAllAlarms(){
        return mAlarmMsgBeanDao.loadAll();
    }



    /**
     * 获取指定时间范围内的所有访客记录
     * */
    public List<RingRecordBean> getTargetRings(String bid, Long minTime, Long maxTime){
        return mRingRecordBeanDao.queryBuilder()
                .where(RingRecordBeanDao.Properties.Ringtime.between(minTime, maxTime),
                        RingRecordBeanDao.Properties.Bid.eq(bid))
                .build().list();
    }
    public List<RingRecordBean> getAllRings(){
        return mRingRecordBeanDao.loadAll();
    }



    /**
     * 插入一条访客记录，即门铃列表
     * */
    public void insertRingMsg(RingRecordBean ring){
        if (ring != null)
            mRingRecordBeanDao.insertOrReplace(ring);
    }

    public void deleteRingMsg(RingRecordBean ringRecordBean){
        if (ringRecordBean != null)
            mRingRecordBeanDao.delete(ringRecordBean);
    }

    public void deleteAllRings(){
        mRingRecordBeanDao.deleteAll();
    }

    public RingRecordBean getRingMsgByBidAndTime(String bid, Long time){
        return mRingRecordBeanDao.queryBuilder().where(RingRecordBeanDao.Properties.Bid.eq(bid),
                RingRecordBeanDao.Properties.Ringtime.eq(time)).build().unique();

    }

    public void deleteRingMsgByIdAndTime(String bid, Long ringtime){
        RingRecordBean ring = getRingMsgByBidAndTime(bid, ringtime);
        if (ring != null)
            mRingRecordBeanDao.delete(ring);
    }
    public void deleteRingMsgByIdAndFid(String bid, String fid){
        RingRecordBean ringRecordBean = mRingRecordBeanDao.queryBuilder().where(RingRecordBeanDao.Properties.Bid.eq(bid),
                RingRecordBeanDao.Properties.Fid.eq(fid)).build().unique();
        if (ringRecordBean != null)
            mRingRecordBeanDao.delete(ringRecordBean);
    }

    /**
     * 获取设备信息
     * name在全局范围内唯一
     * */
    public CateyeInfoBean getCateyeInfo(String name){
        if (StringUtils.isEmpty(name)){
            return null;
        }
        return mCateyeInfoBeanDao.queryBuilder().where(CateyeInfoBeanDao.Properties.Name.eq(name)).build().unique();
    }
    public CateyeInfoBean getCateyeInfoByBid(String bid) {
        if (StringUtils.isEmpty(bid)) {
            return null;
        }
        return mCateyeInfoBeanDao.queryBuilder().where(CateyeInfoBeanDao.Properties.Bid.eq(bid)).build().unique();
    }
    /**
     * 已知bid，获取name
     * */
    public String getCateyeInfoName(String bid){
        if (StringUtils.isEmpty(bid)){
            return null;
        }
        return mCateyeInfoBeanDao.queryBuilder().where(CateyeInfoBeanDao.Properties.Bid.eq(bid)).build().unique().getName();
    }
    /**
     * 更新设备信息
     * */
    public void updateCateyeInfo(CateyeInfoBean infoBean){
        if (infoBean != null)
            mCateyeInfoBeanDao.update(infoBean);
    }

    public void updateNick(String bid, String nick){
        if (StringUtils.isEmpty(bid) || StringUtils.isEmpty(nick)){
            return;
        }
        CateyeInfoBean info =  mCateyeInfoBeanDao.queryBuilder().where(CateyeInfoBeanDao.Properties.Bid.eq(bid)).build().unique();
        info.setNick(nick);
        updateCateyeInfo(info);
    }

    /**插入新的设备*/
    public void insertCateyeInfo(CateyeInfoBean info){
        if (info != null)
            mCateyeInfoBeanDao.insert(info);
    }

    public List<CateyeInfoBean> getAllCateyeInfo(){
        return mCateyeInfoBeanDao.loadAll();
    }

    /**删除设备*/
    public void deleteCateyeInfo(CateyeInfoBean infoBean){
        if (infoBean != null)
            mCateyeInfoBeanDao.delete(infoBean);
    }

    public void deleteAllCateyeInfo(){
        mCateyeInfoBeanDao.deleteAll();
    }
    public void deleteCateyeInfo(String bid){
        CateyeInfoBean info = mCateyeInfoBeanDao.queryBuilder().where(CateyeInfoBeanDao.Properties.Bid.eq(bid)).build().unique();
        if (info != null)
            mCateyeInfoBeanDao.delete(info);
    }
}
